// pages/me_albumAdd/me_albumAdd.js
var app = getApp()
Page({
  data:{
    domain:app.globalData.domain,
    System_Img_Url:app.globalData.System_Img_Url,
    play_img:app.globalData.System_Img_Url+'icon_music_pause.png',
    pause_img:app.globalData.System_Img_Url+'icon_music_play.png',
    music_src:'',
    pic_index:0
  },
  onLoad:function(o){
    if(wx.getStorageSync('album_info_s')){
      var album = wx.getStorageSync('album_info_s')
    //  console.log(album)
      if(album.Album_Background){
        var bg = app.globalData.domain+album.Album_Background
      }else{
        var bg = app.globalData.System_Img_Url+'background.jpg'
      }
      console.log(app.globalData.domain)
      if(album.Album_Border){
        var border = app.globalData.domain+album.Album_Border
      }else{
        var border = app.globalData.System_Img_Url+'border.jpg'
      }
      this.GetPics(album.Album_Id)
      this.setData({
        bg:bg,
        border:border,
        album:album,
        music_img:this.data.pause_img
      })
      var music_src= this.data.domain+album.Album_music
      this.music_play(music_src)
    }else{
      var bg = app.globalData.System_Img_Url+'background.jpg'
      var border = app.globalData.System_Img_Url+'border.jpg'
      console.log(border)
      this.setData({
        bg:bg,
        border:border
      })
    }

    var t = this
    wx.getSystemInfo({
      success:function(e){
        t.setData({
          w_height:e.windowHeight,
          w_width:e.windowWidth
        })
      }
    })

  },

//返回按钮
  go_back(){
    wx.redirectTo({
      url:'../me/me',
    })
  },

//背景音乐的播暂停
  music_play(music_src){
    this.audioCtx = wx.createAudioContext('myAudio')
    this.audioCtx.setSrc(music_src)
    this.audioCtx.play()
  },

  audioPlay: function () {
    this.audioCtx.play()
  },
  audioPause: function () {
    this.audioCtx.pause()
  },

  music_v(){
    if(wx.getStorageSync('music_stat')){
      wx.setStorageSync('music_stat',null)
      this.setData({
        music_img:this.data.pause_img
      })
      this.audioPlay()
    }else{
      wx.setStorageSync('music_stat',1)
      this.setData({
        music_img:this.data.play_img
      })
      this.audioPause()
    }
  },

//获取相册图片
  GetPics(album_id,pnow=1,pcount=0){
    var t = this
    wx.request({
      url:app.globalData.urls,
      data:{
        'GetTypes':'Get_All_Pics',
        'album_id':album_id
      },
      complete:function(res){
        var info = res.data
        t.setData({
          pic_info:info,
        })
        t.set_new_pic_info(t,t.data.pic_index)
      }
    })
  },


  set_new_pic_info(t,index){
    var plength = t.data.pic_info.length
    if(index >= plength ){
      var pinfo = wx.getStorageSync('pic_info')
      t.setData({
        pic_index:0,
        pic_info:pinfo
      })
      if(pinfo.length>0){
        var max = 0;
        for(var i=0;i<pinfo.length;i++){
          if(pinfo[i].ph > max){
            max = pinfo[i].ph
          }
        }
        t.setData({
          pic_index:0,
          pic_info:pinfo,
          pic_max_ph:max
        })
      }else{
        t.setData({
          pic_index:0,
          pic_info:pinfo
        })
      }
      return
    }
    var url = app.globalData.domain + t.data.pic_info[index].Picture_Src
    var w_width = t.data.w_width      //获取屏幕宽度
    wx.getImageInfo({
      src: url,
      complete: function (res) {
        var img_w = res.width       //获取图片的宽高
        var img_h = res.height
        var pw = Math.ceil(w_width*0.9)     //当前图片所在元素的实际宽度
        var scale = (img_w/pw).toFixed(3)   //获取宽度比
        var ph = Math.ceil(img_h/scale)     //获取到当前图片所在元素的实际高度
        var info = t.data.pic_info
        info[index].ph = ph
        info[index].pw = pw
        wx.setStorageSync('pic_info',info)
        index = index + 1  
        t.setData({
          pic_index:index
        }) 
        t.set_new_pic_info(t,index)
      }
    })
  },
  





//页面跳转
  jump(e){
    var t = this
    var type = e.currentTarget.dataset.type;
    var album = wx.getStorageSync("album_info_s")
    switch(type){
      case 'album_name':
        wx.redirectTo({
          url: '../album_name/album_name'
        })
        break;

      case 'album_template':
        if(t.data.album){
          wx.redirectTo({
            url: '../album_background/album_background'
          })
        }else{
          wx.showToast({title:'请先设置相册名！','icon':'loading'})
        }
        break;

      case 'album_border':
        console.log(1)
        if(t.data.album){
          wx.redirectTo({
            url: '../album_border/album_border'
          })
        }else{
          wx.showToast({title:'请先设置相册名！','icon':'loading'})
        }
        break;

      case 'album_music':
        if(t.data.album){
          wx.redirectTo({
            url: '../album_music/album_music'
          })
        }else{
          wx.showToast({title:'请先设置相册名！','icon':'loading'})
        }
        break;

      case 'album_pic':
        if(t.data.album){
          wx.redirectTo({
            url:'../album_pic/album_pic'
          })
        }else{
          wx.showToast({title:'请先设置相册名！','icon':'loading'})
        }
        break;
      case 'album_save':
        if(wx.getStorageSync('album_info_s')){
          if(!album.Album_Background){
            album.Album_Background='/Sites/Site34/Album/system/background.jpg'
          }
          if(!album.Album_Border){
            album.Album_Border='/Sites/Site34/Album/system/border.jpg'
          }
        //  console.log(album)
        //  return
          wx.request({
            url:app.globalData.urls,
            data:{
              'GetTypes':'Album_Change_Info',
              'Album_Id':album.Album_Id,
              'Album_Info':album.Album_Info,
              'Album_Name':album.Album_Name,
              'Album_Pic_Cover':album.Album_Pic_Cover,
              'Album_Pic_Cover_Id':album.Album_Pic_Cover_Id,
              'Album_Background':album.Album_Background,
              'Album_Border':album.Album_Border,
              'Album_Public':album.Album_Public,
              'Album_music':album.Album_music,
              'Album_UserOpenid':album.Album_UserOpenid
              
            },
            success:function(res){
              wx.redirectTo({
                url: '../me/me'
              })
            }
          })
        }else{
          wx.showToast({title:'请先设置相册名！','icon':'loading'})
        }
        break;
    }
  }
})